import {localStore} from "../../Store/localStore";
import request from "../../utils/request.js";
import {openLogin} from "./openLogin.js";

export async function autoLogin() {
    //1.获取token
    const token = localStore.get("token");

    //2.将token发送给服务器 ,验证是否过期

    const headers = {
        "Authorization" : token
    }

    const response = await request.post("/users/auto-login",{},{
        headers
    });

    const result = response.data;

    if(result.code === 0){//自动登录成功


        return {status : true , message : localStore.get("userinfo")}
    }else{//有状况
        if(result.code === 10000){//自动续签
            //把信的token写入到 localStorage
            localStore.set("token",result.data.token);

            return {status : true , message : localStore.get("userinfo")}
        }else{
            //长token也过期了 ， 不在redis 的 0号库当中
            localStore.remove("token");
            localStore.remove("userinfo");
            openLogin();
            //弹开登录组件
            return {status : false , message : result.message}
        }
    }
}
